/**
 *
 */
package de.guidoludwig.jtm.client.shows;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

import de.guidoludwig.jtm.util.StringUtil;

/**
 * @author guido
 *
 */
public abstract class HTMLFormat<T> extends Format {

	protected abstract String getTitle(T t);
	protected static final String NBSP = "&nbsp;";

	@Override
	public final Object parseObject(String source, ParsePosition pos) {
		throw new UnsupportedOperationException();
	}

	@Override
	public final StringBuffer format(Object obj, StringBuffer toAppendTo,
			FieldPosition pos) {
		if (obj == null) {
			return toAppendTo;
		}
		@SuppressWarnings("unchecked")
		T t = (T) obj;
		toAppendTo
				.append("<html>")
				.append("<h1>" + getTitle(t) + "</h1>")
				.append(buildContent(t))
     			.append("<html>");

		return toAppendTo;
	}

	protected abstract String buildContent(T t);

	protected String buildTableRow(String label, String content) {
		return "<tr><th align=\"left\">" + toHTMLLines(label) +
				 "</th><td>" + toHTMLLines(content) + "</td></tr>";
	}

	protected String toHTMLLines(String remarks) {
		if (StringUtil.isBlank(remarks)) {
			return NBSP;
		}
		return remarks.trim().replaceAll("\n", "<br/>");
	}


}
